<?php
/* 
 * 网点报备
 */
class ReportController extends FrontController
{
    private $pagesize = 20;
    /**
     * 报备列表
     * 上级可以审核下级
     */
    public function actionIndex()
    { 
         $p = $this->get('p', 1);
        $network = new NetWork();
        $network_list = array();
        if($this->auth_grade == 1) {//如果是总行,得到下级网点,分行、支行
            $network_list = $network->findAll("bank_id={$this->bank_id}");  
        } else{
            $network_list = $network->getAllNext($this->network_id, $this->auth_grade);
        }
        $network_names = array();
        $network_ids[0] = $this->network_id;
        $network_names[$this->network_id] = $this->network_name;
        if ($network_list) {
            foreach ($network_list as $key => $val) {
                $network_ids[] = $val['network_id'];
                $network_names[$val['network_id']] = $val['network_name'];
            }
        }

        $netreport = new NetworkReport();
        $criteria = new CDbCriteria();
        $criteria->addInCondition('network_id', $network_ids);
        $criteria->addCondition('status!=0');
        //上一级网点才能看到
        //$criteria->addColumnCondition(array('audit' => $this->level -1));
        $total = $netreport->count($criteria);
        
        $criteria->order = 'network_id desc';
        $criteria->limit = $this->pagesize;
        $criteria->offset = ($p - 1) * $this->pagesize;
        $criteria->order = 'create_time desc';
        $list = $netreport->findAll($criteria);
        if ($list) {
            foreach ($list as $key => $val) {
                $list[$key]['project'] = Project::model()->findByPk($val['project_id']);
            }
        }
        $page = new Page($total, $this->pagesize);
        $this->smarty->assign('network_names', $network_names);
        $this->smarty->assign('list', $list);
        $this->smarty->assign('page', $page->style1());
        $this->smarty->assign('network_id', $this->network_id);
        $this->smarty->assign('audit', NetworkReport::$audit);
        $this->smarty->assign('status', NetworkReport::$status);
       $this->smarty->display('report/list.tpl'); 
    }
    /**
     * 添加报备信息
     */
    public function actionAdd()
    {
       $nr_id = (int)$this->get('nr_id');
       if ($nr_id) {
           $info = NetworkReport::model()->findByPk($nr_id)->attributes;
           $this->smarty->assign('info', $info);
           //是否有附件
           $attach = NetworkReportAttach::model()->findAll('nr_id='.$nr_id.' and status=1');
           $this->smarty->assign('attach', $attach);
       }
       //选择项目,当前网点有的项目
       $netquestion = NetworkQuestion::model()->findAll('network_id='.$this->network_id.' and status=1');
       $project_list = array();
       if ($netquestion) {
           $project_ids = array();
           foreach ($netquestion as $key => $val) {
               $project_ids[] = $val['project_id'];
           }
           $project_ids = array_unique($project_ids);
           $project_list = Project::model()->findAllByPk($project_ids);
       }
       $this->smarty->assign('project_list', $project_list);
       $this->smarty->assign('nr_id', $nr_id);
       $this->smarty->assign('network_id', $this->network_id);
       $this->smarty->display('report/add.tpl'); 
    }
    /**
     * 执行添加
     */
    public function actionDoadd()
    {
        $nr_id = (int)$this->post('nr_id');
        $network_id = (int)$this->post('network_id');
        $project_id = (int)$this->post('project_id');
        if ($nr_id) {
            $report = NetworkReport::model()->findByPk($nr_id);
        } else {
            $report = new NetworkReport();
            $report->nr_order = NetworkReport::model()->count("project_id={$project_id} and network_id={$network_id}") + 1;
        }
        $report->project_id = $project_id;
        $report->network_id = $network_id;
        $report->start_time = strtotime($this->post('start_time'));
        $report->end_time = strtotime($this->post('end_time'));
        $report->content = $this->post('content');
        $report->create_time = time();
        $ids = array();
        if ($nar_id = $this->post('nra_id')){
            $ids = explode(',', $nar_id);
            $ids =array_unique($ids);
            if ($ids) {
                $report->haveattach = 1;
            }
        }
        if ($nr_id) {
            $res = $report->update();
        } else {
            $res = $report->save();
        }
        
        if ($res) {
            //成功的话把附件的关联上
            if ($ids) {
                NetworkReportAttach::model()->updateByPk($ids, array('nr_id' => $report->nr_id,'project_id'=>$project_id));
            }
            $this->_ajax(array('status' => 1,'note' =>'保存成功'));
        } else {
            $this->_ajax(array('status' => 0,'note' =>'保存失败'));
        }
    }
    /**
     * 设置状态
     */
    public function actionSetStatus()
    {
        $nr_id = $this->post('nr_id');
        $status = $this->post('status');
        if (count($nr_id)==0) {
            $this->_ajax(array('status' => 0,'note' => '请先选择网点'));
        }
        $status = $this->post('status', 1);
        foreach ($nr_id as $id) {

            $info = NetworkReportAudit::model()->find('nr_id='.$id.' and network_id='.$this->network_id);
            if ($info) {
                NetworkReportAudit::model()->updateByPk($info->nrd_id, array('status' => $status,'create_time' => time()));
            } else {
                $netaudit = new NetworkReportAudit();
                $netaudit->network_id = $this->network_id;
                $netaudit->level = $this->auth_grade;
                $netaudit->nr_id = $id;
                $netaudit->status = $status;
                $netaudit->create_time = time();
                $netaudit->save();
            }

            NetworkReport::model()->updateByPk($id, array('status' => $status,'audit' => $this->auth_grade));
        }
        $this->_ajax(array('status' => 1,'note' => '设置成功'));
    }
    /**
     * 历史记录
     */
    public function actionHistory()
    {
        $nr_id = $this->get('nr_id');
        $criteria = new CDbCriteria();
        $criteria->addColumnCondition(array('nr_id' => $nr_id));
        $criteria->order = 'create_time desc';
        $netaudit = new NetworkReportAudit();
        $list = $netaudit->findAll($criteria);
        if ($list) {
            foreach ($list as $key => $val) {
                $info = NetWork::model()->findByPk($val['network_id'])->attributes;
                $list[$key]['network_name'] = $info['network_name'];
            }
        }
        $this->smarty->assign('level', NetWork::networkLevel());
        $this->smarty->assign('status', NetworkReportAudit::$status);
        $this->smarty->assign('list', $list);
        $this->smarty->display('report/history.tpl');
    }
    /**
     * 删除附件
     */
    public function actionDelAttach()
    {
        $nra_id = (int)$this->get('nra_id');
        if (NetworkReportAttach::model()->updateByPk($nra_id, array('status' => 0))) {
            $this->_ajax(array('status' => 1,'note' => '删除成功'));
        } else {
            $this->_ajax(array('status' => 0,'note' => '删除失败'));
        }
    }
    /**
     * 删除报备
     */
    public function actionDel()
    {
        $nr_id = (int)$this->get('nr_id');
        if (NetworkReport::model()->deleteByPk($nr_id)) {
            $this->_ajax(array('status' => 1,'note' => '删除成功'));
        } else {
            $this->_ajax(array('status' => 0,'note' => '删除失败'));
        } 
    }
}
